package com.noble.meether.mail.ui.friend.request;

import android.content.Context;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.bean.center.user.light.UserLightList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.view.base.BasePanel;
import com.noble.library.view.custom.state.CustomStatusListView;
import com.noble.library.widget.ListView.xlistview.ExListView;
import com.noble.meether.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 好友申请panel
 */
public class FriendRequestPanel extends BasePanel implements RequestComplete, ExListView.IXListViewListener, PObserver {
    private CustomStatusListView customStatusListView;
    private ExListView exListView;

    private MyFriendRequestAdapter adapter;
    private List<UserLight> data = new ArrayList<>();

    private int page = 1;

    public FriendRequestPanel(Context context) {
        super(context);
        setContentView(R.layout.panel_request_friend);
        initView();
        onRefresh();
    }


    private void initView() {
        customStatusListView = (CustomStatusListView) findViewById(R.id.myfriend_list);
        exListView = customStatusListView.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        adapter = new MyFriendRequestAdapter(getContext(), data);
        exListView.setAdapter(adapter);
        customStatusListView.showLoading();
        MsgMgr.getInstance().attach(this);
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getCommonMgr().getMyRequestFriends(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getCommonMgr().getMyRequestFriends(page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            UserLightList infoList = (UserLightList) response.getBaseData();
            if (infoList.getLightList().size() != 0) {
                if (page == 1) {
                    if (data.size() != 0) {
                        data.clear();
                    }
                }
                data.addAll(infoList.getLightList());
                if (data.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
            } else {
                if (page == 1) {
                    customStatusListView.showNoData("暂无好友申请");
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }

            if (page == 1) {
                exListView.stopRefresh();
            } else {
                exListView.stopLoadMore();
            }
        } else {
            customStatusListView.showNoData("请求出错", "重试", view -> {
                customStatusListView.showLoading();
                onRefresh();
            });
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Update_Friend:
                try {
                    HashMap<String, Object> param = (HashMap<String, Object>) value;
                    long uidTo = (long) param.get("uid_to");
                    int status_friend = (int) param.get("status_friend");
                    if (status_friend == 1 || status_friend == -4) {
                        for (UserLight userLight : data) {
                            if (userLight.getUid() == uidTo) {
                                data.remove(userLight);
                                break;
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    public void release() {
        MsgMgr.getInstance().detach(this);
    }
}